﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Big2.CardsLib;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Runtime;


namespace Big2.Big2Game
{
    [Serializable]
    public class Hand
    {
        public List<Card> CardsList { get; set; }
        public List<Card> SelectedCards { get; set; }

        public Hand()
        {
            CardsList = new List<Card>();
            SelectedCards = new List<Card>();
        }

        /*
        //compare hand to see which one is bigger
        //return 1 if left
        //return -1 if right
        //if equal compare suits
        public int CompareHand(Play hand)
        {
            //change value
            CardHelper.ChangeValue(this.CardsList, true);
            CardHelper.ChangeValue(hand.CardsList, true);

            //assumes the hand is valid already
            //compares to see which hands wins

            //comparing single , pair and 3 of a kind is similar -- compares only first value
            if (hand.ThePlayType == PlayType.Single || hand.ThePlayType == PlayType.Pair ||
                hand.ThePlayType == PlayType.ThreeOfAKind)
            {
                if (this.CardsList.ElementAt(0).Value > hand.CardsList.ElementAt(0).Value)
                    return returnResult(this.CardsList, hand.CardsList, 1);
                else if (this.CardsList.ElementAt(0).Value < hand.CardsList.ElementAt(0).Value)
                    return returnResult(this.CardsList, hand.CardsList, -1);
                else if (this.CardsList.ElementAt(0).Value == hand.CardsList.ElementAt(0).Value)
                {
                    if (this.CardsList.ElementAt(0).Suit > hand.CardsList.ElementAt(0).Suit)
                        return returnResult(this.CardsList, hand.CardsList, 1);
                    else
                        return returnResult(this.CardsList, hand.CardsList, -1);
                }//compare suits
            }//if is single, pair or three of kind

            //comparing full house and 4 of a kind -- comparing middle card
            else if (hand.ThePlayType == PlayType.FullHouse)
            {
                //compares the 3 card because it can be 2-3 or 3-2
                //or 4 of a kind 1-4 or 4-1

                if (this.CardsList.ElementAt(2).Value > hand.CardsList.ElementAt(2).Value)
                    return returnResult(this.CardsList, hand.CardsList, 1);
                else if (this.CardsList.ElementAt(2).Value < hand.CardsList.ElementAt(2).Value)
                    return returnResult(this.CardsList, hand.CardsList, -1);
            }//if is full house

            //comparing straight -- compares last card
            else if (hand.ThePlayType == PlayType.Straight)
            {
                if (this.CardsList.ElementAt(4).Value > hand.CardsList.ElementAt(4).Value)
                    return returnResult(this.CardsList, hand.CardsList, 1);
                else if (this.CardsList.ElementAt(4).Value < hand.CardsList.ElementAt(4).Value)
                    return returnResult(this.CardsList, hand.CardsList, -1);
                else if (this.CardsList.ElementAt(4).Value == hand.CardsList.ElementAt(4).Value)
                {
                    if (this.CardsList.ElementAt(0).Suit > hand.CardsList.ElementAt(0).Suit)
                        return returnResult(this.CardsList, hand.CardsList, 1);
                    else
                        return returnResult(this.CardsList, hand.CardsList, -1);
                }//compare suits
            }//if is straight

            //comparing flush
            else if (hand.ThePlayType == PlayType.Flush)
            {
                if (this.CardsList.ElementAt(0).Suit > hand.CardsList.ElementAt(0).Suit)
                    return returnResult(this.CardsList, hand.CardsList, 1);
                else if (this.CardsList.ElementAt(0).Suit < hand.CardsList.ElementAt(0).Suit)
                    return returnResult(this.CardsList, hand.CardsList, -1);
                if (this.CardsList.ElementAt(0).Suit == hand.CardsList.ElementAt(0).Suit)
                {
                    if (this.CardsList.ElementAt(4).Value > hand.CardsList.ElementAt(4).Value)
                        return returnResult(this.CardsList, hand.CardsList, 1);
                    else
                        return returnResult(this.CardsList, hand.CardsList, -1);
                }//compare last value
            }//if flush

            //comparing straight flush
            else if (hand.ThePlayType == PlayType.StraightFlush)
            {
                if ((this.CardsList.ElementAt(4).Value > hand.CardsList.ElementAt(4).Value) ||
                    (this.CardsList.ElementAt(4).Suit > hand.CardsList.ElementAt(4).Suit))
                    return returnResult(this.CardsList, hand.CardsList, 1);
                else if ((this.CardsList.ElementAt(4).Value < hand.CardsList.ElementAt(4).Value) ||
                    (this.CardsList.ElementAt(4).Suit < hand.CardsList.ElementAt(4).Suit))
                    return returnResult(this.CardsList, hand.CardsList, -1);
            }//if straight flush

            throw new System.Exception("Invalid comparison of 2 hands");
        }//CompareHand(PlayType hand)

        //method to able to change value of ace and 2 to original value and
        //able to return without resulting in more code
        private int returnResult(List<Card> first, List<Card> second, int r)
        {
            //change value back
            CardHelper.ChangeValue(first, false);
            CardHelper.ChangeValue(second, false);
            return r;
        }//int returnResult(int r)
        */









        public int CardsLeft()
        {
            return CardsList.Count();
        }//CardsLeft()

        public void AddCardToHand(Card c)
        {
            //TODO: sort?
            CardsList.Add(c);
        }
        public void RemoveCardFromHand(Card c)
        {
            CardsList.Remove(c);
        }

    }
}
